package chapter21.section2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @date 2020-11-05 下午5:06
 */
public class InnerThread1 {
    private final Inner inner;
    private int countDown = 5;

    public InnerThread1(String name) {
        inner = new Inner(name);
    }

    /**
     * 使用具名内部类
     */
    private class Inner extends Thread {
        Inner(String name) {
            super(name);
            start();
        }

        @Override
        public void run() {
            while (true) {
                try {
                    System.out.println(this);
                    if (--countDown == 0) {
                        return;
                    }
                    sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public String toString() {
            return getName() + ": " + countDown;
        }
    }
}
